﻿using Model.Public;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Service.Table;
using System.Data;
using MySql.Data.MySqlClient;

namespace Model.User
{
    public class Game : BaseClass<Game>
    {
        public int Id;

        public int UID;

        public string GuildName;

        public string GuildIcon;

        public int NumShipTrainStart;

        public int NumQuestFinished;

        public int NumProducts;

        public int NumHelp;

        public int NumShare;

        public int NumSeaConnect;

        public int NumGameEvent;

        public int NumUnlockTech;

        public int CityProsperity;

        public int DataFlag;

        public DateTime CreateTime;

        #region 方法

        public const string DB_Name = "Game";  //redis角色表key

        /// 获取DB
        /// <summary>
        /// 获取DB
        /// </summary>
        /// <returns></returns>
        public string GetDBName()
        {
            return DB_Name + "_" + this.UID.ToString();
        }

        /// 动态设置DB的key
        /// <summary>
        /// 动态设置DB的key
        /// </summary>
        /// <param name="db_key"></param>
        public void SetDBName(string hash_id)
        {
            this.UID = Convert.ToInt32(hash_id);
        }

        /// 获取hash key
        /// <summary>
        /// 获取hash key
        /// </summary>
        /// <returns></returns>
        public int GetKey()
        {
            return this.Id;
        }

        /// 设置hash key
        /// <summary>
        /// 设置hash key
        /// </summary>
        /// <param name="key"></param>
        public void SetKey(int key)
        {
            this.Id = key;
        }

        public int GetUID()
        {
            return this.UID;
        }

        public Dictionary<string, object> ModelChangeToDic()
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            //dic.Add("UID",               UID);
            dic.Add("GuildName",         GuildName);
            dic.Add("GuildIcon",         GuildIcon);
            dic.Add("NumShipTrainStart", NumShipTrainStart);
            dic.Add("NumQuestFinished",  NumQuestFinished);
            dic.Add("NumProducts",       NumProducts);
            dic.Add("NumHelp",           NumHelp);
            dic.Add("NumShare",          NumShare);
            dic.Add("NumSeaConnect",     NumSeaConnect);
            dic.Add("NumGameEvent",      NumGameEvent);
            dic.Add("NumUnlockTech",     NumUnlockTech);
            dic.Add("CityProsperity",    CityProsperity);
            //dic.Add("DataFlag",          DataFlag);

            return dic;
        }

        public void DicChangeToModel(Dictionary<string, object> dic)
        {
            UID = dic.ContainsKey("UID") ? int.Parse(dic["UID"].ToString()) : 0;
            GuildName = dic.ContainsKey("GuildName") ? dic["GuildName"].ToString() : "";
            GuildIcon = dic.ContainsKey("GuildIcon") ? dic["GuildIcon"].ToString() : "";
            NumShipTrainStart = dic.ContainsKey("NumShipTrainStart") ? int.Parse(dic["NumShipTrainStart"].ToString()) : 0;
            NumQuestFinished  = dic.ContainsKey("NumQuestFinished") ? int.Parse(dic["NumQuestFinished"].ToString()) : 0;
            NumProducts    = dic.ContainsKey("NumProducts") ? int.Parse(dic["NumProducts"].ToString()) : 0;
            NumHelp        = dic.ContainsKey("NumHelp") ? int.Parse(dic["NumHelp"].ToString()) : 0;
            NumShare       = dic.ContainsKey("NumShare") ? int.Parse(dic["NumShare"].ToString()) : 0;
            NumSeaConnect  = dic.ContainsKey("NumSeaConnect") ? int.Parse(dic["NumSeaConnect"].ToString()) : 0;
            NumGameEvent   = dic.ContainsKey("NumGameEvent") ? int.Parse(dic["NumGameEvent"].ToString()) : 0;
            NumUnlockTech  = dic.ContainsKey("NumUnlockTech") ? int.Parse(dic["NumUnlockTech"].ToString()) : 0;
            CityProsperity = dic.ContainsKey("CityProsperity") ? int.Parse(dic["CityProsperity"].ToString()) : 0;
            DataFlag       = dic.ContainsKey("DataFlag") ? int.Parse(dic["DataFlag"].ToString()) : 1;
        }

        /// 新增数据
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <returns></returns>
        public int InsertData()
        {
            string sqlStr = @"insert into game(UID, GuildName, GuildIcon, NumShipTrainStart, NumQuestFinished, NumProducts, NumHelp, NumShare, NumSeaConnect, NumGameEvent, NumUnlockTech, CityProsperity, DataFlag, CreateTime)
                              values(?UID, ?GuildName, ?GuildIcon, ?NumShipTrainStart, ?NumQuestFinished, ?NumProducts, ?NumHelp, ?NumShare, ?NumSeaConnect, ?NumGameEvent, ?NumUnlockTech, ?CityProsperity, ?DataFlag, now());
                              select @@Identity; ";
            MySqlParameter[] param = new MySqlParameter[] {
                new MySqlParameter("?UID",               UID),
                new MySqlParameter("?GuildName",         GuildName),
                new MySqlParameter("?GuildIcon",         GuildIcon),
                new MySqlParameter("?NumShipTrainStart", NumShipTrainStart),
                new MySqlParameter("?NumQuestFinished",  NumQuestFinished),
                new MySqlParameter("?NumProducts",       NumProducts),
                new MySqlParameter("?NumHelp",           NumHelp),
                new MySqlParameter("?NumShare",          NumShare),
                new MySqlParameter("?NumSeaConnect",     NumSeaConnect),
                new MySqlParameter("?NumGameEvent",      NumGameEvent),
                new MySqlParameter("?NumUnlockTech",     NumUnlockTech),
                new MySqlParameter("?CityProsperity",    CityProsperity),
                new MySqlParameter("?DataFlag",          DataFlag),
            };
            var ret = MySQLDataControl.ExecuteScalar(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param);
            return ret != null && ret != DBNull.Value ? Convert.ToInt32(ret) : 0;
        }

        /// 更新数据
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <returns></returns>
        public bool UpdateData()
        {
            string sqlStr = @"update game set GuildName=?GuildName, GuildIcon=?GuildIcon, NumShipTrainStart=?NumShipTrainStart,
                 NumQuestFinished=?NumQuestFinished, NumProducts=?NumProducts, NumHelp=?NumHelp, NumShare=?NumShare,
                 NumSeaConnect=?NumSeaConnect, NumGameEvent=?NumGameEvent,CityProsperity=?CityProsperity, DataFlag=?DataFlag where UID=?UID  ";
            MySqlParameter[] param = new MySqlParameter[] {
                new MySqlParameter("?UID",               UID),
                new MySqlParameter("?GuildName",         GuildName),
                new MySqlParameter("?GuildIcon",         GuildIcon),
                new MySqlParameter("?NumShipTrainStart", NumShipTrainStart),
                new MySqlParameter("?NumQuestFinished",  NumQuestFinished),
                new MySqlParameter("?NumProducts",       NumProducts),
                new MySqlParameter("?NumHelp",           NumHelp),
                new MySqlParameter("?NumShare",          NumShare),
                new MySqlParameter("?NumSeaConnect",     NumSeaConnect),
                new MySqlParameter("?NumGameEvent",      NumGameEvent),
                new MySqlParameter("?NumUnlockTech",     NumUnlockTech),
                new MySqlParameter("?CityProsperity",    CityProsperity),
                new MySqlParameter("?DataFlag",          DataFlag),
            };
            return MySQLDataControl.ExecuteNonQuery(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param) > 0;
        }

        /// 查询
        /// <summary>
        /// 查询
        /// </summary>
        public bool SelectData(Dictionary<string, object> p)
        {
            bool result = false;
            string sqlStr = @"select UID, GuildName, GuildIcon, NumShipTrainStart, NumQuestFinished, NumProducts, NumHelp, NumShare, NumSeaConnect, NumGameEvent, NumUnlockTech, CityProsperity, DataFlag, CreateTime
                                          from game where UID = ?UID  limit 1 ";
            MySqlParameter[] param = new MySqlParameter[] {
                            new MySqlParameter("?UID", Convert.ToInt32(p["UID"]))
                        };
            DataTable dt = MySQLDataControl.ExecuteDataTable(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param);
            if (dt.Rows.Count > 0)
            {
                DRChangeToModel(dt.Rows[0]);
                result = true;
            }
            return result;
        }

        /// 查询列表数据的SQL语句
        /// <summary>
        /// 查询列表数据的SQL语句
        /// </summary>
        /// <returns></returns>
        public List<Game> GetDataList(Dictionary<string, object> p)
        {
            string sqlStr = @"select UID, GuildName, GuildIcon, NumShipTrainStart, NumQuestFinished, NumProducts, NumHelp, NumShare, NumSeaConnect, NumGameEvent, NumUnlockTech, CityProsperity, DataFlag, CreateTime
                              from game where UID = ?UID and DataFlag = 1";
            MySqlParameter[] param = new MySqlParameter[] {
                new MySqlParameter("?UID", Convert.ToInt32(p["UID"]))
            };
            DataTable dt = MySQLDataControl.ExecuteDataTable(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param);
            List<Game> list = new List<Game>(dt.Rows.Count);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Game quest = new Game();
                    quest.DRChangeToModel(dt.Rows[i]);

                    list.Add(quest);
                }
            }
            return list;
        }

        /// datarow 转 model
        /// <summary>
        /// datarow 转 model
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public void DRChangeToModel(DataRow dr)
        {
            this.UID = Convert.ToInt32(dr["UID"]);
            this.GuildName = dr["GuildName"].ToString();
            this.GuildIcon = dr["GuildIcon"].ToString();
            this.NumShipTrainStart = Convert.ToInt32(dr["NumShipTrainStart"]);
            this.NumQuestFinished  = Convert.ToInt32(dr["NumQuestFinished"]);
            this.NumProducts    = Convert.ToInt32(dr["NumProducts"]);
            this.NumHelp        = Convert.ToInt32(dr["NumHelp"]);
            this.NumShare       = Convert.ToInt32(dr["NumShare"]);
            this.NumSeaConnect  = Convert.ToInt32(dr["NumSeaConnect"]);
            this.NumGameEvent   = Convert.ToInt32(dr["NumGameEvent"]);
            this.NumUnlockTech  = Convert.ToInt32(dr["NumUnlockTech"]);
            this.CityProsperity = Convert.ToInt32(dr["CityProsperity"]);
            this.DataFlag       = Convert.ToInt32(dr["DataFlag"]);
        }

        /// 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public bool DeleteData(Dictionary<string, object> p)
        {
            string sqlStr = @"update game set DataFlag = 0  where Id = ?Id ";
            MySqlParameter[] param = new MySqlParameter[] {
                new MySqlParameter("?UID", Convert.ToInt32(p["UID"]))
            };
            return MySQLDataControl.ExecuteNonQuery(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param) > 0;
        }

        /// 获取同一个账号下的数据个数
        /// <summary>
        /// 获取同一个账号下的数据个数
        /// </summary>
        /// <returns></returns>
        public int GetCount(Dictionary<string, object> p)
        {
            string sqlStr = @"select count(1) from game where UID = ?UID and DataFlag = 1 ";
            MySqlParameter[] param = new MySqlParameter[] {
                new MySqlParameter("?UID", Convert.ToInt32(p["UID"]))
            };
            var ret = MySQLDataControl.ExecuteScalar(MySQLDataControl.GetConnection(), CommandType.Text, sqlStr, param);
            return ret != null && ret != DBNull.Value ? Convert.ToInt32(ret) : 0;
        }

        #endregion
    }
}